import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:znmm/UI/utils/sharedpre_utils.dart';

import '../strings.dart';
import '../app_theme.dart';

part 'app_theme_state.dart';

//Theme样式
class AppThemeCubit extends Cubit<AppThemeState> {
  AppThemeCubit(AppTheme initialTheme) : super(AppThemeInitial(initialTheme)) {
    loadTheme();
  }
  void loadTheme() {
    String? themeName = SharedPrefUtil().getData(SharedKey.strThemeKey);

    final AppTheme theme = themeName != null
        ? c_appThemes.keys.firstWhere(
            (theme) => theme.name.toLowerCase() == themeName.toLowerCase(),
            orElse: () => c_appThemes.keys.first,
          )
        : c_appThemes.keys.first;
    emit(AppThemeUpdate(theme));
  }

  void updateTheme(AppTheme newTheme) {
    SharedPrefUtil().setData(SharedKey.strThemeKey, c_appThemes[newTheme]!);

    emit(AppThemeUpdate(newTheme));
  }
}
